import requests
from bs4 import BeautifulSoup
import json
def save_to_json(data, filename='news_hotsearch.json'):
    try:
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=4)
        print(f"数据已成功保存到 {filename}")
    except IOError as e:
        print(f"保存文件失败: {e}")
#爬虫的代码
def parse_html(html):
    # 发送HTTP请求
    url = html  # 替换为你的目标URL
    response = requests.get(url)
    # 检查请求是否成功
    if response.status_code == 200:
        # 解析HTML文档
        soup = BeautifulSoup(response.text, 'html.parser')
        # 新闻要点
        elements1 = soup.select('.hotnews  a')
        # 热搜新闻词
        elements2 = soup.select('.bd  a')
        data = []
        # 假设 elements1 和 elements2 长度相同，一一对应
        min_length = min(len(elements1), len(elements2))
        for i in range(min_length):
            item1 = elements1[i]
            item2 = elements2[i]
            data.append({
                "新闻要点": item1.get_text(strip=True),
                "热搜新闻词": item2.get_text(strip=True)
            })
        return data
    else:
        print(f"请求失败，状态码：{response.status_code}")
#主函数
def main():
    data = parse_html("https://news.baidu.com/")
    save_to_json(data)

if __name__ == "__main__":
    main()
